package com.akaovi.shareWare.utils;

import com.akaovi.shareWare.gui.ProcessBar;
import com.akaovi.shareWare.mess.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Properties;

/**
 * @author akaovi
 * @version 1.0
 * @description 工具类
 * @date 2022/10/13 23:23
 * @since 2022/10/13 23:23
 */
public class Utils {

    private static final Logger logger = LoggerFactory.getLogger(Utils.class);

    private static final InetAddress selfAddr;

    private static final int port;

    private static final int corePoolSize;

    private static final int maxPoolSize;

    private static final String logPath;

    private static final String saveFilePath;

    private static final String filePoolPath;

    private static final String rootPath;

    private static final int maxCacheSize;

    static {
        InetAddress selfAddr1;
        try {
            selfAddr1 = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            selfAddr1 = null;
            logger.error("Utils Init selfAddr Error >>> ", e);
        }
        selfAddr = selfAddr1;

        InputStream is = null;
        int port1;
        int corePoolSize1;
        int maxPoolSize1;
        String logPath1;
        String saveFilePath1;
        String filePoolPath1;
        String rootPath1;
        int maxCacheSize1;
        try {
            is = Utils.class.getClassLoader().getResourceAsStream("config.properties");
            Properties properties = new Properties();
            properties.load(is);

            port1 = Integer.parseInt(properties.getProperty("defaultPort"));
            corePoolSize1 = Integer.parseInt(properties.getProperty("corePoolSize"));
            maxPoolSize1 = Integer.parseInt(properties.getProperty("maxPoolSize"));
            logPath1 = properties.getProperty("defaultLogFilePath");
            saveFilePath1 = properties.getProperty("defaultServerFilePath");
            filePoolPath1 = properties.getProperty("filePoolPath");
            rootPath1 = properties.getProperty("rootPath");
            maxCacheSize1 = Integer.parseInt(properties.getProperty("maxCacheSize"));

        } catch (Exception e) {
            logger.error("Properties Init Error >>> ", e);
            port1 = 17777;
            corePoolSize1 = 7;
            maxPoolSize1 = 17;
            logPath1 = "D:/sharedFiles/log";
            saveFilePath1 = "D:/sharedFiles/saveFile";
            filePoolPath1 = "D:/sharedFiles/filePool";
            rootPath1 = "D:/sharedFiles";
            maxCacheSize1 = 10;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    logger.error("Close Stream Error >>> ", e);
                }
            }
        }
        port = port1;
        corePoolSize = corePoolSize1;
        maxPoolSize = maxPoolSize1;
        logPath = logPath1;
        saveFilePath = saveFilePath1;
        filePoolPath = filePoolPath1;
        rootPath = rootPath1;
        maxCacheSize = maxCacheSize1;
    }

    public static int getPort() {
        return port;
    }

    public static int getCorePoolSize() {
        return corePoolSize;
    }

    public static int getMaxPoolSize() {
        return maxPoolSize;
    }

    public static String getLogPath() {
        return logPath;
    }

    public static String getSaveFilePath() {
        return saveFilePath;
    }

    public static String getFilePoolPath() {
        return filePoolPath;
    }

    public static String getRootPath() {
        return rootPath;
    }

    public static int getMaxCacheSize() {
        return maxCacheSize;
    }

    /**
     * 获取自己的ip地址
     *
     * @return InetAddress
     */
    public static InetAddress getSelfAddr() {
        return selfAddr;
    }

    /**
     * 判断字符串是否为空
     *
     * @param s
     * @return boolean
     */
    public static boolean isEmpty(String s) {
        return (s == null) || ("".equals(s));
    }

    public static InetAddress getIpAddress(String s) {
        try {
            return InetAddress.getByName(s);
        } catch (UnknownHostException e) {
            return null;
        }
    }

    public static FileMess getFileMess(String filePath) {
        File file = new File(filePath);
        return new FileMess(ContentType.FileType, file.length(), file.getName());
    }

    public static TextMess getTextMess(String text) {
        return new TextMess(ContentType.TextType, text);
    }

    public static CacheMess getCacheMess(LRUCache<String> cacheText) {
        return new CacheMess(ContentType.PullTextType, cacheText);
    }

    public static PullFileMess getPullFileMess(String fileName) {
        return new PullFileMess(ContentType.PullFileType, fileName);
    }

    public static boolean isIllegal(String s) {
        switch (s.trim()) {
            case "1":
            case "6":
            case "10":
            case "2":
            case "3":
            case "4":
            case "5":
            case "7":
            case "8":
            case "9":
                return true;
            default:
                return false;
        }
    }

    public static void createBaseDir() {
        File rootFile = new File(Utils.getRootPath());
        if (!rootFile.exists()) {
            rootFile.mkdirs();
        }

        File saveFile = new File(rootFile, "saveFile");
        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }

        File filePool = new File(rootFile, "filePool");
        if (!filePool.exists()) {
            filePool.mkdirs();
        }

        File logFile = new File(rootFile, "log");
        if (!logFile.exists()) {
            logFile.mkdirs();
        }
    }

    public static void closeProcessBar(ProcessBar bar) {
        if (bar != null) {
            bar.dispose();
        }
    }

    public static void closeDataOutputStream(DataOutputStream dos) {
        if (dos != null) {
            try {
                dos.close();
            } catch (IOException e) {
                logger.error("dos Close Error >>> ", e);
            }
        }
    }

    public static void closeDataInputStream(DataInputStream dis) {
        if (dis != null) {
            try {
                dis.close();
            } catch (IOException e) {
                logger.error("dis Close Error >>> ", e);
            }
        }
    }

    public static void closeObjectOutputStream(ObjectOutputStream oos) {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                logger.error("oos Close Error >>> ", e);
            }
        }
    }

    public static void closeObjectInputStream(ObjectInputStream ois) {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                logger.error("ois Close Error >>> ", e);
            }
        }
    }

    public static void closeSocket(Socket socket) {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                logger.error("socket Close Error >>> ", e);
            }
        }
    }

    public static void closeServerSocket(ServerSocket serverSocket) {
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                logger.error("serverSocket {} Close Error >>> ", serverSocket, e);
            }
        }
    }
}
